package cn.micai.base.collections;

import java.util.*;

/**
 * 描述：How to iterate a list in java
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/11 15:44
 */
public class IterateListMain {

    public static void main(String [] args) {

        Country india = new Country("India",1000);
        Country japan = new Country("Japan",10000);
        Country france = new Country("France",2000);
        Country russia = new Country("Russia",20000);

        // We are going to iterate on this list and will print
        //name of the country
        ArrayList<Country> countryLists = new ArrayList();
        countryLists.add(india);
        countryLists.add(japan);
        countryLists.add(france);
        countryLists.add(russia);

        // Sort by countryName
        /*Collections.sort(countryLists, new Comparator<Country>() {
            @Override
            public int compare(Country o1, Country o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });*/

        // Using lambda expression
        /*Collections.sort(countryLists, (Country c1, Country c2) -> c1.getName().compareTo(c2.getName()));*/
        Collections.sort(countryLists, (c1, c2) -> c1.getName().compareTo(c2.getName()));

        // For loop
        /*System.out.println("Iterating using for loop : ");
        for (int i=0; i<countryLists.size(); i++) {
            Country country = (Country) countryLists.get(i);
            System.out.println(country.getName());
        }
        System.out.println("-----------------------------");*/

        // For each loop
        /*System.out.println("Iterating using for each loop : ");
        for (Country country : countryLists) {
            System.out.println(country.getName());
        }
        System.out.println("-----------------------------");*/

        // While loop
        /*System.out.println("Iterating using while loop : ");
        int i =0;
        while (i < countryLists.size()) {
            Country country = countryLists.get(i);
            System.out.println(country.getName());
            i++;
        }
        System.out.println("-----------------------------");*/

        // Iterator
        System.out.println("Iterating using iterator : ");
        List<String> stringList = new ArrayList<>();
        Iterator<Country> iterator = countryLists.iterator();
        while (iterator.hasNext()) {
            Country country = iterator.next();
            //System.out.println(country.getName());
            if (country.getName().equals("Japan")) {
                iterator.remove();
                continue;
            }
            stringList.add(country.getName());
        }
        System.out.println(stringList.toString());
        System.out.println("-----------------------------");

    }
}
